/**
 * Created by coder on 2016/3/14.
 */
'use strict'

let models = require('../modules/models');
let Fiber = require('fibers');

let AttendanceCachePersonTimeInfo = models.AttendanceCachePersonTimeInfo;
let AttendanceReportCache = models.AttendanceReportCache;

let SchoolDao = models.SchoolDao;
let GradeDao = models.GradeDao;
let NatureClassDao = models.NatureClassDao;
let StudentDao = models.StudentDao;
let AttendanceCachePersonTimeInfoDao = models.AttendanceCachePersonTimeInfoDao;
let AttendanceReportCacheDao = models.AttendanceReportCacheDao;
let AttendanceDao = models.AttendanceDao;

function createDailyReport(date) {

    let schools = SchoolDao.find({});

    //date.setHours(0,0,0,0);
    let oneday = 1000 * 60 * 60 * 24;
    let error = 1000 * 60;

    schools.forEach(school=>{

        let schoolCacheInfo = new AttendanceCachePersonTimeInfo();
        schoolCacheInfo = initializeInfo(schoolCacheInfo);
        school.grades.forEach(grade=>{
            let gradeCacheInfo = new AttendanceCachePersonTimeInfo();
            gradeCacheInfo = initializeInfo(gradeCacheInfo);
            grade.natureClasses.forEach(natureClass=>{

                let classCacheInfo = new AttendanceCachePersonTimeInfo();
                classCacheInfo = initializeInfo(classCacheInfo);

                natureClass.students.forEach(student=>{
                    //console.log(student.id);
                    let studentAttendance = AttendanceDao.findOne({
                        date:{
                            $gte: new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, -1, 0),
                            $lte: new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 1, 0)
                        },
                        student:student.id
                    });
                    if(studentAttendance !== null){
                        //console.log(111);
                        let studentCacheInfo = countStudentAttendance(studentAttendance.values);
                        classCacheInfo = countPersonTimeInfo(classCacheInfo,studentCacheInfo);
                    }

                });

                AttendanceCachePersonTimeInfoDao.create(classCacheInfo);
                let attendanceClassReportCache = new AttendanceReportCache();
                //attendanceReportCache.periodType = 2;
                attendanceClassReportCache.date =  new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
                attendanceClassReportCache.attendanceScope = 2;
                attendanceClassReportCache.school = school;
                attendanceClassReportCache.grade = grade;
                attendanceClassReportCache.natureClass = natureClass;
                attendanceClassReportCache.personTimeInfo = classCacheInfo;
                AttendanceReportCacheDao.create(attendanceClassReportCache);

                gradeCacheInfo = countPersonTimeInfo(gradeCacheInfo,classCacheInfo);

            });

            //console.log(gradeCacheInfo.absenceCount);
            AttendanceCachePersonTimeInfoDao.create(gradeCacheInfo);
            let attendanceGradeReportCache = new AttendanceReportCache();
            //attendanceReportCache.periodType = 2;
            attendanceGradeReportCache.date = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
            attendanceGradeReportCache.attendanceScope = 1;
            attendanceGradeReportCache.school = school;
            attendanceGradeReportCache.grade = grade;
            //attendanceGradeReportCachehe.natureClass = null;
            attendanceGradeReportCache.personTimeInfo = gradeCacheInfo;
            AttendanceReportCacheDao.create(attendanceGradeReportCache);

            schoolCacheInfo = countPersonTimeInfo(schoolCacheInfo,gradeCacheInfo);
        });

        AttendanceCachePersonTimeInfoDao.create(schoolCacheInfo);
        let attendanceSchoolReportCache = new AttendanceReportCache();
        //attendanceReportCache.periodType = 2;
        attendanceSchoolReportCache.date = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
        attendanceSchoolReportCache.attendanceScope = 0;
        attendanceSchoolReportCache.school = school;
        //attendanceReportCache.grade = null;
        //attendanceReportCache.natureClass = null;
        attendanceSchoolReportCache.personTimeInfo = schoolCacheInfo;
        AttendanceReportCacheDao.create(attendanceSchoolReportCache);

    });
}

function countPersonTimeInfo(personTimeInfo1,personTimeInfo2){
    personTimeInfo1.absenceCount += personTimeInfo2.absenceCount;
    personTimeInfo1.earlyCount += personTimeInfo2.earlyCount;
    personTimeInfo1.attendCount += personTimeInfo2.attendCount;
    personTimeInfo1.lateCount += personTimeInfo2.lateCount;
    personTimeInfo1.leaveCount += personTimeInfo2.leaveCount;
    return personTimeInfo1;
}

function countStudentAttendance(attendanceValues){
    let studentCacheInfo = new AttendanceCachePersonTimeInfo();
    studentCacheInfo = initializeInfo(studentCacheInfo);
    attendanceValues.forEach(attendanceValue=>{
        //console.log(1);
        //console.log(attendanceValue.enterStatus);
        switch (attendanceValue.enterStatus){
            case 1:
                studentCacheInfo.attendCount++;
                break;
            case 3:
                studentCacheInfo.leaveCount++;
                break;
            case -1:
                studentCacheInfo.absenceCount++;
                break;
            case -2:
                studentCacheInfo.lateCount++;
                break;
            case -3:
                studentCacheInfo.earlyCount++;
                break;
        }
        switch (attendanceValue.exitStatus){
            case 1:
                studentCacheInfo.attendCount++;
                break;
            case 3:
                studentCacheInfo.leaveCount++;
                break;
            case -1:
                studentCacheInfo.absenceCount++;
                break;
            case -2:
                studentCacheInfo.lateCount++;
                break;
            case -3:
                studentCacheInfo.earlyCount++;
                break;
        }
    });
    if(studentCacheInfo.attendCount === 6){
        studentCacheInfo.attendCount = 1;
    }
    else{
        studentCacheInfo.attendCount = 0;
    }

    return studentCacheInfo;
}

function initializeInfo(info){
    info.absenceCount = 0;
    info.earlyCount = 0;
    info.attendCount = 0;
    info.lateCount = 0;
    info.leaveCount = 0;
    return info;
}

Fiber(function() {

    let date = new Date(2016,2,20,0,0,0,0);
    createDailyReport(date);

    //let now = new Date();

    //let startTime = new Date(2016, 2, 15, 0, -1, 0);
    //let endTime = new Date(2016, 2, 15, 0, 1, 0);
    //
    //let a = AttendanceDao.find({
    //    date: {
    //        $gte: startTime,
    //        $lte: endTime
    //    }
    //});
    //console.log(a);

    process.exit();
}).run();